using System;
using System.Collections.Generic;
using System.Text;
using CreamX.Framework.Game.Components;
using CreamX.Framework.Attributes;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;

namespace Copter.Components
{
    [CreamXComponentAttribute("CopterControllerComponent")]
    public class CopterControllerComponent:CreamXComponent
    {
        CreamXPhysicsComponent Physics;
        public CopterControllerComponent()
        {
        }
        public override void OnRegister()
        {
            Physics = SceneObject.GetComponent<CreamXPhysicsComponent>();
            Physics.Velocity = new Microsoft.Xna.Framework.Vector2(Speed, -50);
            Enabled = true;
            base.OnRegister();
        }
        public override void Update(float elapsed)
        {
            if(!ApplyInput(elapsed))
                ApplyGravity(elapsed);

            if (Physics.VelocityY > MaxVerticalSpeed)
                Physics.VelocityY = MaxVerticalSpeed;
            if (Physics.VelocityY < -MaxVerticalSpeed)
                Physics.VelocityY = -MaxVerticalSpeed;

            
            CurrentScene.Camera.SetPosition(new Vector2(SceneObject.Position.X - 250, 
                CurrentScene.Camera.Position.Y));
            base.Update(elapsed);
        }

        private bool ApplyInput(float elapsed)
        {
            KeyboardState _input = Keyboard.GetState();
            if (_input.IsKeyDown(Keys.Space))
            {
                Physics.VelocityY -= PowerStrength * elapsed;
                return true;
            }
            return false;
        }

        private void ApplyGravity(float elapsed)
        {
            Physics.VelocityY += GravityStrength * elapsed;
        }
        private int _speed=5;
        [CreamXComponentProperty("Speed")]
        public int Speed
        {
            get { return _speed; }
            set { _speed = value; }
        }
        private int _gravity = 5;
        [CreamXComponentProperty("GravityStrength")]
        public int GravityStrength
        {
            get { return _gravity; }
            set { _gravity = value; }
        }

        private int _power = 5;
        [CreamXComponentProperty("PowerStrength")]
        public int PowerStrength
        {
            get { return _power; }
            set { _power = value; }
        }

        private int _vertspeed = 5;
        [CreamXComponentProperty("MaxVerticalSpeed")]
        public int MaxVerticalSpeed
        {
            get { return _vertspeed; }
            set { _vertspeed = value; }
        }
    }
}
